package com.china08.yxyapi.api.manage.tec.office.meetingv1_1;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.meeting.VideoMeeting;
import com.china08.yxyapi.entity.db.meeting.VideoMeetingFile;
import com.china08.yxyapi.entity.db.meeting.VideoMeetingUser;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.VideoMeetingFileRepository;
import com.china08.yxyapi.repository.db.VideoMeetingRepository;
import com.china08.yxyapi.repository.db.VideoMeetingUserRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.DateUtils;

/**
 * 视频会议详细信息
 *
 */
@RestController("tea_meeting_detail")
public class VideoMeetingv1_1DetailController extends YxyApiBaseController {

    @Autowired
    private VideoMeetingRepository videoMeetingRepository;

    @Autowired
    private VideoMeetingFileRepository videoMeetingFileRepository;

    @Autowired
    private VideoMeetingUserRepository videoMeetingUserRepository;

    @RequestMapping(value = "/api_yw/manage/tea/office/meetingv1_1/videomeet/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws ParseException {

        YxyProfile yxyProfile = getProfile(request, response);
        String schooId = yxyProfile.getSchoolId();

        RespModel model = new RespModel();
        VideoMeeting vMeeting = videoMeetingRepository.findOne(reqModel.id);

        Assert.isTrue(vMeeting != null, "该视频会议不存在");

        model.id = vMeeting.getId();
        model.createdDate = DateUtils.formatDate8(vMeeting.getCreatedDate());
        model.createdByName = vMeeting.getSchoolName() + "-" + vMeeting.getCreatedByName();
        model.status = vMeeting.getStatus();
        model.title = vMeeting.getTitle();
        String date = DateUtils.formatDate2(vMeeting.getStartDate());
        String start = DateUtils.formatDate11(vMeeting.getStartDate());
        String end = DateUtils.formatDate11(vMeeting.getEndDate());
        model.date = date + " " + start + "-" + end;
        model.speakerName = vMeeting.getSpeakerName();
        model.moblie = vMeeting.getSpeakerMobile();
        model.intro = vMeeting.getIntro();

        model.peopleNum = vMeeting.getActualPeopleNum();// v1.1

        // v1.1
        List<VideoMeetingUser> meetingUserLst = videoMeetingUserRepository
                .findByMeetingIdOrderByUserType(vMeeting.getId());
        List<String> meetingUsersLst = new ArrayList<String>();
        for (VideoMeetingUser meetingUser : meetingUserLst) {
            meetingUsersLst.add(meetingUser.getUserNick());
        }
        model.meetingUsersLst = meetingUsersLst;
        model.meetingLabel = vMeeting.getMeetingLabel();

        if (model.status == -1) {
            model.cause = vMeeting.getCause();
            model.causeDate = DateUtils.formatDate2(vMeeting.getLastModifiedDate());
        } else {
            long difference = DateUtils.difference2(vMeeting.getStartDate(), new Date());
            if ((difference <= 30 && vMeeting.getStatus() == 0) || vMeeting.getStatus() == 1) {
                model.getInto = 1;
            } else {
                model.getInto = 0;
            }
        }

        if (reqModel.channel == 1) {
            model.teacherCode = vMeeting.getTeacherCode();
            model.studentCode = vMeeting.getStudentCode();
        }

        VideoMeetingFile file = videoMeetingFileRepository.findByMeetingId(vMeeting.getId());
        if (file != null) {
            model.attachment = file.getAttachment();
            model.attachmentName = file.getAttachmentName();
        }
        return model;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiPageReqModel {
        // 会议id
        public String id;
        // 进入渠道 0-视频会议查看，1-会议管理查看
        public Integer channel = 0;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "会议编号不能为空");
        }
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {
        // id
        public String id;
        // 发布时间
        public String createdDate;
        // 发布人
        public String createdByName;
        // 状态
        public Integer status;
        // 会议主题
        public String title;
        // 会议时间
        public String date;
        // 主讲人
        public String speakerName;
        // 手机号
        public String moblie;
        // 会议内容
        public String intro;
        // 附件路径
        public String attachment;
        // 附件名称
        public String attachmentName;
        // 会议取消原因
        public String cause;
        // 会议取消时间
        public String causeDate;
        // 主讲人参加码
        public String teacherCode;
        // 参会人参加码
        public String studentCode;
        // 能否进入会议 0-不能，1-能
        public Integer getInto;

        // 会议类型
        public String meetingLabel;// v1.1
        // 参会人列表
        public List<String> meetingUsersLst;// v1.1
        // 参会人数
        public Integer peopleNum; // v1.1
    }
}
